// ShaderUtils.js
var ShaderUtils = {
    shaderPrograms: {},
    setShader: function(sprite, shaderName) {
        
        this.glProgram = this.shaderPrograms[shaderName];
        if (!this.glProgram) {
            this.glProgram = new cc.GLProgram();
            // var vert = require(cc.js.formatStr("%s.vert", shaderName));
            // var frag = require(cc.js.formatStr("%s.frag", shaderName));

            var vert = require(cc.js.formatStr("%s2d.vert", shaderName));
            var frag = require(cc.js.formatStr("%s2d.frag", shaderName));
            if (cc.sys.isNative) {  
                this.glProgram.initWithString(vert, frag);
            } else {  
                this.glProgram.initWithVertexShaderByteArray(vert, frag);
                this.glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION);  
                this.glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_COLOR, cc.macro.VERTEX_ATTRIB_COLOR);  
                this.glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_TEX_COORD, cc.macro.VERTEX_ATTRIB_TEX_COORDS);  
            }
            this.glProgram.link();  
            this.glProgram.updateUniforms();

            // this._uniAngle = this.glProgram.getUniformLocationForName( "angle" );
            // this._uniRadius = this.glProgram.getUniformLocationForName( "radius" );

            this._uniTime = this.glProgram.getUniformLocationForName( "iTime" );
            this._uniWaveDensity = this.glProgram.getUniformLocationForName( "waveDensity" );
            this._uniWaveRate = this.glProgram.getUniformLocationForName( "waveRate" );
            this._uniScreenWidth = this.glProgram.getUniformLocationForName( "screenWidth" );
            this._uniScreenHeight = this.glProgram.getUniformLocationForName( "screenHeight" );

            this.shaderPrograms[shaderName] = this.glProgram;

        }
        sprite._sgNode.setShaderProgram(this.glProgram);
    },

    update: function(angle, radius){
        // this.glProgram.setUniformLocationWith1f( this._uniAngle, angle );
        // this.glProgram.setUniformLocationWith1f( this._uniRadius, radius );
        this.glProgram.updateUniforms();
    },

    updateWave2D: function(time, waveDensity, waveRate, screenSize){
        this.glProgram.updateUniforms();
        this.glProgram.setUniformLocationWith1f( this._uniTime, time );
        this.glProgram.setUniformLocationWith1f( this._uniWaveDensity, waveDensity );
        this.glProgram.setUniformLocationWith1f( this._uniWaveRate, waveRate );
        this.glProgram.setUniformLocationWith1f( this._uniScreenWidth, screenSize.width);
        this.glProgram.setUniformLocationWith1f( this._uniScreenHeight, screenSize.height);
        // console.log("width: " + screenSize.width + ", height: " + screenSize.height);
        
    },

    
};

module.exports = ShaderUtils;